package org.huqiwen.blog.control;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.huqiwen.blog.logic.CommentLogicBean;

public class CommentActionServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String method = (String) request.getParameter("method");
		if (method == null) {
			PrintWriter out = response.getWriter();
			out.println("无效请求");
		} else if (method.equals("addComment")) {
			AddComment(request, response);
		} else if (method.equals("delComment")) {
			DelComment(request, response);
		}
	}

	protected void AddComment(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String commentPostId = request.getParameter("blogId");
		String commentName = request.getParameter("name");
		String commentEmail = request.getParameter("email");
		String commentMainpage = request.getParameter("mainpage");
		String commentIp = request.getRemoteAddr();
		String commentContent = request.getParameter("content");
		int result = new CommentLogicBean().addComment(commentPostId,
				commentName, commentEmail, commentMainpage, commentIp,
				commentContent);
		if (result == 1) {
			response.sendRedirect("BlogActionServlet?method=detailBlog&id=" + commentPostId);
			// request.getRequestDispatcher("FindByCatServlet?cat="+commentPostId).forward(request,
			// response);
		} else {
			request.getRequestDispatcher("error.jsp")
					.forward(request, response);
		}
	}

	protected void DelComment(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String commId = request.getParameter("id");
		String blogId = request.getParameter("blogId");
		int result = new CommentLogicBean().delComment(commId);
		if (result == 1) {
			response.sendRedirect("BlogActionServlet?method=detailBlog&id=" + blogId);
		} else {
			response.sendRedirect("error.jsp");
		}
	}
}
